home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / spatial / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.7 KB  |  187 lines

  1. /* (C) Copyright 1991 Andrew Plotkin. Permission is
  2.  given to copy and use, as long as this copyright
  3.  notice is retained. */
  4.  
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <ctype.h> 
  8. #include <pwd.h>    
  9. #include <unistd.h>    
  10. #include <sys/types.h>
  11. #include <sys/file.h>
  12. #include <X11/Xlib.h>
  13. #ifdef AFS
  14. #include <niftyprofile.h>    
  15. #endif
  16. #include "spatial.h"
  17.  
  18. extern char *getenv();
  19. char ProgramName[] = "spatial";
  20.  
  21. gamer hscores[NUMGAMERS];
  22. char hscorefl[512];
  23. char userid[16], gamename[16];
  24.  
  25. extern int errno;
  26.  
  27. void load_hscores(save)
  28. int save;
  29. {
  30.     FILE *fl;
  31.     register int ix, jx;
  32.     int fd, res;
  33.     char buf[255];
  34.  
  35.     fl = fopen(hscorefl, "r+");
  36.     if (!fl) {
  37.     fprintf(stderr,
  38.         "spatial: unable to open high score file\n");
  39.     fprintf(stderr,
  40.         "spatial: generating empty score file\n");
  41.  
  42.     fl = fopen(hscorefl, "w");
  43.     if (!fl) {
  44.         fprintf(stderr,
  45.             "spatial: unable to create high score file\n");
  46.         exit(-1);
  47.     }
  48.  
  49.     for (ix=0; ix<NUMGAMERS; ix++) {
  50.         fprintf(fl, "----\nSpatial\n0\n");
  51.     }
  52.     fclose(fl);
  53.     fl = fopen(hscorefl, "r+");
  54.     if (!fl) {
  55.         fprintf(stderr,
  56.             "spatial: unable to reopen high score file\n");
  57.         exit(-1);
  58.     }
  59.     }
  60.  
  61.     fd = fileno(fl);
  62. #ifdef USELOCKF
  63.     res = lockf(fd, F_LOCK, 0);
  64. #else
  65.     res = flock(fd, LOCK_NB | LOCK_EX);
  66. #endif
  67.     while (res) {
  68.     if (errno != EWOULDBLOCK) {
  69.         fprintf(stderr,
  70.             "spatial: unable to lock high score file\n");
  71.         exit(-1);
  72.     };
  73.     sleep(1);
  74. #ifdef USELOCKF
  75.     res = lockf(fd, F_LOCK, 0);
  76. #else
  77.     res = flock(fd, LOCK_NB | LOCK_EX);
  78. #endif
  79.     };
  80.  
  81.     for (ix=0; ix<NUMGAMERS; ix++) {
  82.     long val;
  83.  
  84.     fgets(buf, 255, fl);
  85.     for (jx=0; jx<15 && buf[jx]!='\n'; jx++) 
  86.         hscores[ix].userid[jx] = buf[jx];
  87.     hscores[ix].userid[jx] = '\0';
  88.     
  89.     fgets(buf, 255, fl);
  90.     for (jx=0; jx<15 && buf[jx]!='\n'; jx++) 
  91.         hscores[ix].name[jx] = buf[jx];
  92.     hscores[ix].name[jx] = '\0';
  93.     
  94.     fgets(buf, 255, fl);
  95.     val=0;
  96.     for (jx=0; buf[jx]!='\n'; jx++) 
  97.         val = val*10 + buf[jx] - '0';
  98.     hscores[ix].score = val;
  99.     sprintf(hscores[ix].scoret, "%d", val);
  100.     };
  101.  
  102.     if (save) {
  103.     if (checkmod_scores()) {
  104.         rewind(fl);
  105.         for (ix=0; ix<NUMGAMERS; ix++) {
  106.         fprintf(fl, "%s\n%s\n%d\n", hscores[ix].userid,
  107.             hscores[ix].name, hscores[ix].score);
  108.         }
  109.     }
  110.     }
  111.  
  112. #ifdef USELOCKF
  113.     lockf(fd, F_ULOCK, 0);
  114. #else
  115.     flock(fd, LOCK_UN);
  116. #endif
  117.  
  118.     fclose(fl);
  119. }
  120.  
  121. int checkmod_scores()
  122. {
  123.     register int ix;
  124.     int new, bottom;
  125.     
  126.     if (score <= hscores[NUMGAMERS-1].score) return 0;
  127.     
  128.     for (ix=0; strcmp(hscores[ix].userid, userid)
  129.       && ix<NUMGAMERS; ix++);
  130.     
  131.     if (ix<NUMGAMERS) { /* was in list */
  132.     if (score <= hscores[ix].score) return 0;
  133.     bottom = ix;
  134.     }
  135.     else { /* wasn't in list */
  136.     bottom = NUMGAMERS-1;
  137.     };
  138.  
  139.     for (new=0; score<=hscores[new].score; new++);
  140.     for (ix=bottom; ix>new; ix--) {
  141.     strcpy(hscores[ix].name, hscores[ix-1].name);
  142.     strcpy(hscores[ix].userid, hscores[ix-1].userid);
  143.     strcpy(hscores[ix].scoret, hscores[ix-1].scoret);
  144.     hscores[ix].score = hscores[ix-1].score;
  145.     };
  146.     strcpy(hscores[new].name, gamename);
  147.     strcpy(hscores[new].userid, userid);
  148.     hscores[new].score = score;
  149.     sprintf(hscores[new].scoret, "%d", score);
  150.  
  151.     return 1;
  152. }
  153.  
  154. void get_names()
  155. {
  156.     register int ix;
  157.     struct passwd *tp;
  158.     char *tcp;
  159.  
  160.     strcpy(hscorefl, SCOREFILENAME);
  161.  
  162.     tp = getpwuid(getuid());
  163.     if (!tp) {
  164.     printf("unable to get userid!\n");
  165.     strcpy(userid, "????");
  166.     }
  167.     else {
  168.     strncpy(userid, tp->pw_name, 16);
  169.     userid[15] = '\0';
  170.     }
  171.  
  172.     tcp = getenv("NAME");
  173.     if (tcp) {
  174.     strncpy(gamename, tcp, 16);
  175.     gamename[15] = '\0';
  176.     }
  177.     else if (tcp=XGetDefault(dpy, "spatial", "name")) {
  178.     strncpy(gamename, tcp, 16);
  179.     gamename[15] = '\0';
  180.     }
  181.     else {
  182.     strcpy(gamename, userid);
  183.     };
  184.  
  185.     printf("Welcome, %s (%s)\n", gamename, userid);
  186. }
  187.